﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public sealed class Cloud : MonoBehaviour {

        static Transform _cloudLeftSide;

        static Transform CloudLeftSide {
            get {
                if (_cloudLeftSide == null) {
                    _cloudLeftSide = GameObject.FindWithTag("CloudLeftSide").transform;
                }
                return _cloudLeftSide;
            }
        }

        static Transform _cloudRightSide;

        static Transform CloudRightSide {
            get {
                if (_cloudRightSide == null) {
                    _cloudRightSide = GameObject.FindWithTag("CloudRightSide").transform;
                }
                return _cloudRightSide;
            }
        }

        [Range(0f, 20f)]
        public float m_Speed = 1f;

        public bool m_IsLeft = true;

        void Update() {

            transform.position += (m_IsLeft ? Vector3.left : Vector3.right) * m_Speed * Time.deltaTime;

            if (CloudLeftSide == null || CloudRightSide == null) {
                return;
            }

            if (m_IsLeft) {
                if (transform.position.x < CloudLeftSide.position.x) {
                    var pos = transform.position;
                    pos.x = CloudRightSide.position.x;
                    transform.position = pos;
                }
            } else {
                if (transform.position.x > CloudRightSide.position.x) {
                    var pos = transform.position;
                    pos.x = CloudLeftSide.position.x;
                    transform.position = pos;
                }
            }

        }



    }

}
